﻿using System;
using System.IO;
using System.Text;

namespace ExcelConfig
{
    using ExcelConfig.Config;
    using ExcelConfig.helpers;

    public class ExcelConfigHelper
    {
        internal static void non_password_encrypt(ref string password) { }
        internal static void auto_clone_default_configfile()
        {
            if (!File.Exists(AppConfig.ExcelConfig.FullPath))
            {
                if (!Directory.Exists(Path.GetDirectoryName(AppConfig.ExcelConfig.FullPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(AppConfig.ExcelConfig.FullPath));
                }

                using (var defaultfile = AppConfig.EmptyStream())
                using (var fs = File.Create(AppConfig.ExcelConfig.FullPath)) 
                    helper.ReadAllToMemory(defaultfile, fs);
            }
        }
        internal static void show_entertip(ExcelConfigFile cfg, string configname)
        {
            Console.WriteLine($"excelconfig({cfg.Name})# set {configname}");
            Console.Write($"enter new value: ");
        }
        public static void SetDefault(string configname, string default_value)
        {
            if (string.IsNullOrEmpty(AppConfig.ExcelConfig.ReadText(AppConfig.SheetName, configname)))
            {
                AppConfig.ExcelConfig.WriteValue(AppConfig.SheetName, configname, default_value);
            }
        }
        public static void SetDefault(ExcelConfigFile cfg, string configname, string default_value)
        {
            if (string.IsNullOrEmpty(cfg.ReadText(AppConfig.SheetName, configname)))
            {
                cfg.WriteValue(AppConfig.SheetName, configname, default_value);
            }
        }
        public static void SetDefault(AppConfigFile cfg, string configname, string default_value)
        {
            if (string.IsNullOrEmpty(cfg.ExcelConfig.ReadText(AppConfig.SheetName, configname)))
            {
                cfg.ExcelConfig.WriteValue(AppConfig.SheetName, configname, default_value);
            }
        }
        public static void Enter(string configname, bool must = false)
        {
            if (must || string.IsNullOrEmpty(AppConfig.ExcelConfig.ReadText(AppConfig.SheetName, configname)))
            {
                show_entertip(AppConfig.ExcelConfig, configname);
                var v = Console.ReadLine();
                if (v != null && (v = v.Trim()) != string.Empty) AppConfig.ExcelConfig.WriteValue(AppConfig.SheetName, configname, v);
            }
        }
        public static void EnterPassword(string configname, bool must = false)
        {
            auto_clone_default_configfile();

            if (must || string.IsNullOrEmpty(AppConfig.ExcelConfig.ReadText(AppConfig.SheetName, configname)))
            {
                show_entertip(AppConfig.ExcelConfig, configname);
                var v = _ReadPassword();
                if (v != null && (v = v.Trim()) != string.Empty) AppConfig.ExcelConfig.WriteValue(AppConfig.SheetName, configname, v);
            }
        }
        public static void Enter(ExcelConfigFile cfg, string configname, bool must = false)
        {
            if (must || string.IsNullOrEmpty(cfg.ReadText(cfg.SheetName, configname)))
            {
                show_entertip(cfg, configname);
                var v = Console.ReadLine();
                if (v != null && (v = v.Trim()) != string.Empty) cfg.WriteValue(cfg.SheetName, configname, v);
            }
        }
        public static void EnterPassword(ExcelConfigFile cfg, string configname, bool must = false)
        {
            if (must || string.IsNullOrEmpty(cfg.ReadText(cfg.SheetName, configname)))
            {
                show_entertip(cfg, configname);
                var v = _ReadPassword();
                if (v != null && (v = v.Trim()) != string.Empty) cfg.WriteValue(cfg.SheetName, configname, v);
            }
        }
        public static void Enter(AppConfigFile cfg, string configname, bool must = false)
        {
            if (must || string.IsNullOrEmpty(cfg.ExcelConfig.ReadText(cfg.SheetName, configname)))
            {
                show_entertip(cfg.ExcelConfig, configname);
                var v = Console.ReadLine();
                if (v != null && (v = v.Trim()) != string.Empty) cfg.ExcelConfig.WriteValue(cfg.SheetName, configname, v);
            }
        }
        public static void EnterPassword(AppConfigFile cfg, string configname, bool must = false)
        {
            if (must || string.IsNullOrEmpty(cfg.ExcelConfig.ReadText(cfg.SheetName, configname)))
            {
                show_entertip(cfg.ExcelConfig, configname);
                var v = _ReadPassword();
                if (v != null && (v = v.Trim()) != string.Empty) cfg.ExcelConfig.WriteValue(cfg.SheetName, configname, v);
            }
        }
        internal static string _ReadPassword(char tihuan = '*')
        {
            Console.InputEncoding = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;

            var password = new StringBuilder();
            while (true)
            {
                var key = Console.ReadKey(true);
                switch (key.Key)
                {
                    case ConsoleKey.Enter:
                        Console.WriteLine();
                        var _password = password.ToString();
                        if (password.Length != 0) ExcelConfigSetting.PasswordEncrypt(ref _password);
                        return _password;
                    case ConsoleKey.Backspace:
                        if (password.Length == 0) continue;

                        password.Length--;
                        Console.Write("\b \b");
                        continue;
                    default:
                        if (char.IsControl(key.KeyChar)) continue;

                        password.Append(key.KeyChar);
                        Console.Write(tihuan);
                        continue;
                }
            }
        }
    }
}
